package com.wangzaiplus.test.juc.thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName SynDemo1
 * @Description: TODO
 * @Author wgc
 * @Date 2020/06/28 18:05
 * @Version V1.0
 **/
public class ReentrantLockDemo1 {
    private static int num=0;
    private  static ReentrantLock lock=new ReentrantLock();
    private  static  void add(){
        lock.lock();
        lock.lock();
        try {
            num++;
        }finally {
            lock.unlock();
            lock.unlock();
        }

    }


    public static class T1 extends  Thread{
        @Override
        public void run() {
            for (int i = 0; i <10000; i++) {
                ReentrantLockDemo1.add();
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
            T1 t1 = new T1();
            T1 t2 = new T1();
            T1 t3 = new T1();
            t1.start();
            t2.start();
            t3.start();
            //等待3个线程结束打印num
            t1.join();
            t2.join();
            t3.join();
        System.out.println(ReentrantLockDemo1.num);
        }
}
